Git Kata 4: Merge Conflicts
Learn about the merging of two branches where the differences can't be auto-merged.
Step 1: Modify the file in the main branch#
To append to the file and save:
- Add
(I want ripple chips)toCrispy Chips.Crispy Chips (I want ripple chips)- Save the changes.
This step adds a new comment to the Crispy Chips line.
The command to commit the change to the repository is given below.
The output will be something like this:
Command's Parameters
Command / Parameter | Description |
| This creates a new commit in the current branch. |
| This stages all the modified files prior to the commit. |
| This sets a commit message from the command line. |
| This is the commit message. |
This step commits the change made to the Crispy Chips line.
The output of the command is:
Output
Message | Meaning |
"[master 238996b] I want ripple chips" | This is the branch and abbreviated hash of the commit and the commit message. |
"1 file changed, 1 insertion(+), 1 deletion(-)" | This is the number of files changed and the number of insertions and deletions. |
Step 2: Modify the file innewbranch#
The command to switch to newbranch is given below.
When we execute the command above, the output will be something like this:
Command's Parameters
Command / Parameter | Description |
| This checks out a commit or switches to a branch. |
| This is the branch to checkout. |
This command switches to newbranch.
To append in the file and save:
- Switch to the text editor and reload the file.
- Add
(I’ll take barbecue)toCrispy Chips:Crispy Chips (I’ll take barbecue)
This step makes a conflicting change in newbranch to the same Crispy Chips line we modified in the first step. There are now conflicting versions of the same line of the same file between main and newbranch.
The command to commit the change to the repository is given below.
The result will be something like this:
Command's Parameters
Command / Parameter | Description |
| This creates a new commit in the current branch. |
| This stages all the modified files prior to the commit. |
| This sets a commit message from the command line. |
| This is the commit message. |
This step commits the change made in newbranch to the Crispy Chips line of storelist.txt.
The command to switch to main is given below.
The result will be something like this:
Command's Parameters
Command / Parameter | Description |
| This checks out a commit or switches to a branch. |
| This is the branch to checkout. |
This step switches the current branch back to main.
The command to display the difference between the branches is given below.
After running the command above, the output will be something like this:
Command's Parameters
Command / Parameter | Description |
| This displays the difference between objects, including commits, branches, the working tree, and index. |
| This execution of |
Once again, git diff displays the differences between main and newbranch. The storelist.txt file has been modified again, and we see the current differences in the versions between branches.
This output displays two separate difference hunks: the Yummy Yogurt line is still different in the newbranch version, while the other hunk shows the difference we just created in the Crispy Chips line.
The output includes:
Output
Message | Meaning |
"@@ -1,5 +1,5 @@" | This is the first difference from the previous step. Five lines, starting from line 1 and ending on line 5, are displayed. `Yummy Yogurt` has the `I love yogurt` comment in |
"@@ -13,4 +13,4 @@" | This is the second difference in storelist.txt. It extends four lines from line 13. This is the difference we created on the `Crispy Chips` line. Both versions, |
Step 3: Merge and resolve conflicts#
The command to switch to newbranch is given below.
The output will be something like this:
Commands
Command / Parameter | Description |
| This applies commits from one branch to another branch. |
| This is the branch from which we take commits and apply them to the current branch. |
The Git merge command attempts to merge newbranch and main. This execution is different from the last git merge in the previous step because the Crispy Chips line has conflicting changes in both branches. Git cannot auto-merge, so the output indicates that there’s a merge conflict.
Reload the file by switching to the text editor and reloading storelist.txt.
The first step in resolving a merge conflict is to review the conflict headers added by Git to the file. Git annotates the file with markers indicating the version of the Crispy Chips line in main (indicated by HEAD, because main is the current branch) and newbranch.
The conflict headers are formatted as follows:
Conflict Headers
Header | Description |
<<<<<<< HEAD | The following lines, up to the equals signs, represent the version in |
Crispy Chips (I want ripple chips) | This is the version of the `Crispy Chips` line in |
======= | This is a separator between the |
Crispy Chips (I’ll take barbecue) | This is the version of the `Crispy Chips` line in |
<<<<<<< newbranch | This indicates the branch of the version below the equals-line separator. |
We remove the conflict headers and combine the comments: Crispy Chips (Ripple chips and barbecue).
The merge conflict is resolved by editing the line with the conflict (a single line in this example, but can be multiple lines). This situation calls for a compromise—the coders will be buying both Ripple and barbecue chips.
The command to commit the change to the repository is given below.
After the execution of the command above, the output will be something like this:
Command's Parameters
Command / Parameter | Description |
| This creates a new commit in the current branch. |
| This stages all the modified files prior to the commit. |
| This sets a commit message from the command line. |
| This is the commit message. |
The git merge operation in the previous step didn’t create a new commit because the merge conflict had to be resolved first. Git marked the file with conflict headers to assist us with making the desired changes. We had to take it from there and edit the file by hand. Once the desired changes were made, they could be committed.
The previous Git merge command didn’t create a commit; however, it did create a pending merge. This Git commit then creates a new merge commit.
The command to display the repository log in graph format is given below:
The output will be something like this:
Command's Parameters
Command / Parameter | Description |
| This displays the commit history of the current repository. |
| This is the option to display the log as a graph. |
| This is the option to display each commit on a single line. |
The git log command is used to view the commit history of a repository.
The --graph and --pretty=oneline options display the history in a vertical graph format, similar to the horizontal graphs we’ve been using to visualize the commit history and branching. If we flip this vertical graph, we can see that it has essentially the same shape as our horizontal visualization:
Vertical graph representations are commonly used in graphical Git tools to display long histories.
Practice commands#
We’ve given a terminal and a table containing a list of commands discussed in this lesson. Try out these commands after running the terminal, and check out the results!
Step | Command |
This appends |
|
This commits the change to the repository, setting the commit comment from the command line. |
|
This switches to |
|
|
|
This commits the change to the repository, setting the commit comment from the command line. |
|
This switches to |
|
This displays the differences between |
|
This merges |
|
|
|
This comments the change to the repository, setting the commit comment from the command line. |
|
This displays the repository log in graph format. |
|
Git Kata 3: Merging
Git Kata 5: Run a Git Server